package com.hmall.item.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import com.hmall.pojo.ItemCondition;
import com.hmall.pojo.ItemConditionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public PageDTO<Item> pageQuery(Integer page, Integer size) {
        IPage<Item> ipage=new Page<>(page,size);
//        IPage<Item> itemIPage = itemMapper.selectPage(ipage, null);
//        long total = itemIPage.getTotal();
//        List<Item> records = itemIPage.getRecords();
        this.page(ipage, null);
        return new PageDTO<Item>(ipage.getTotal(),ipage.getRecords());
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @Override
    public Item getItemById(Integer id) {
        Item item = itemMapper.selectById(id);
        return item;
    }

    @Override
    public void modifyStatus(Integer id, Integer status) {
        itemMapper.updateById(Item.builder().id(Long.valueOf(id)).status(status).build());
    }

    /**
     * 新增商品
     * @param item
     */
    @Override
    public void addItem(Item item) {
        this.save(item);
    }

    /**
     * 根据id删除商品
     * @param id
     */
    @Override
    public void deleteItemById(Integer id) {
        this.removeById(id);
    }

    /**
     * 过滤项聚合功能
     * @param itemCondition
     * @return
     */
    @Override
    public ItemConditionVo itemByCondition(ItemCondition itemCondition) {
        List list = itemMapper.itemByCondition();
        List list1 = itemMapper.itemByCondition2();
        ItemConditionVo itemConditionVo = new ItemConditionVo(list, list1);
//        List list1=new ArrayList<>();
//        List list2=new ArrayList<>();
//        for (ItemConditionVo itemConditionVo : itemConditionVos) {
//            list1.add(itemConditionVo.getBrandTo());
//            list2.add(itemConditionVo.getCategoryTo());
//        }
        return itemConditionVo;
    }

    /**
     * 基本搜索功能
     * @param itemCondition
     * @return
     */
    @Override
    public PageDTO pageQuery(ItemCondition itemCondition) {
        IPage iPage=new Page(itemCondition.getPage(),itemCondition.getSize());
        LambdaQueryWrapper<Item> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(itemCondition.getKey() !=null && itemCondition.getKey()!=""){
            lambdaQueryWrapper.like(Item::getName, itemCondition.getKey());
        }
        if (itemCondition.getCategory() !=null && itemCondition.getCategory()!=""){
            lambdaQueryWrapper.eq(Item::getCategory,itemCondition.getCategory());
        }
        if (itemCondition.getBrand() !=null && itemCondition.getBrand() !=""){
            lambdaQueryWrapper.eq(Item::getBrand,itemCondition.getBrand());
        }
        if (itemCondition.getMaxPrice() !=null && itemCondition.getMinPrice() !=null){
            lambdaQueryWrapper.between(Item::getPrice,itemCondition.getMinPrice(),itemCondition.getMaxPrice());
        }
        if (itemCondition.getSortBy() !=null && itemCondition.getSortBy() !="" && "price".equals(itemCondition.getSortBy())){
            lambdaQueryWrapper.orderByDesc(Item::getPrice);
        }
        if (itemCondition.getSortBy() !=null && itemCondition.getSortBy() !="" && "sold".equals(itemCondition.getSortBy())){
            lambdaQueryWrapper.orderByDesc(Item::getSold);
        }
        this.page(iPage, lambdaQueryWrapper);
        List records = iPage.getRecords();
        List result = (List) records.stream().sorted(Comparator.comparing(Item::getSold).reversed()).collect(Collectors.toList());
        String key="top";
        redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(result));
        String string = redisTemplate.opsForValue().get(key);
        List<Item> list = JSONUtil.toList(string, Item.class);
        return new PageDTO<>(iPage.getTotal(),list);
    }

    @Scheduled(cron = "0 0 3 * * ?")
    public void reload(){
        String key="top";
        LambdaQueryWrapper<Item> itemLambdaQueryWrapper = Wrappers.<Item>lambdaQuery().orderByDesc(Item::getSold);
        List<Object> result =itemMapper.selectObjs(itemLambdaQueryWrapper);
        redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(result));
    }

}
